package cn.net.autocode.platform.providers;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 获取字典信息
 */
@RestController
@RequestMapping("autocode/dictProvider")
public class DictProviderCtrl {

    private final Dao dao;

    /**
     * 初始化，注入默认Dao实例
     * @param dao 数据库操作对象
     */
    public DictProviderCtrl(Dao dao) {
        this.dao = dao;
    }

    /**
     * 根据字典组信息，获取字典组下的字典值及显示值
     * @param dictArray 字典组信息
     * @return 字典值及显示信息
     */
    @RequestMapping("options")
    public ResultT options(@RequestBody JSONArray dictArray){
        //查询所有的状态信息
        JSONObject dictOptions = new JSONObject();
        int size = dictArray.size();
        for(int i=0;i<size;i++){
            String dictCode = dictArray.getString(i);
            JSONArray dictItems = dao.queryJSONArray("select t1.code value,t1.label,t1.font_color,t1.icon,t1.icon_color from sys_dict_item t1 where t1.dict_code=? and t1.enable='Y' order by t1.sort_no",dictCode);
            dictOptions.put(dictCode, dictItems);
        }
        return ResultT.success(dictOptions);
    }

    /**
     * 根据字典组信息，获取字典组下的字典值及显示值,只能传递一个值
     * @param dictArray 字典组信息
     * @return 字典值及显示信息
     */
    @RequestMapping("items")
    public ResultT items(@RequestBody JSONArray dictArray){
        int size = dictArray.size();
        if(size==0){
            throw new RuntimeException("dictCode不能为空");
        }else if(size==1){
            String dictCode = dictArray.getString(0);
            JSONArray dictItems = dao.queryJSONArray("select code value,label from sys_dict_item where dict_code=? and enable='Y' order by sort_no",dictCode);
            return ResultT.success(dictItems);
        }else{
            throw new RuntimeException("dictCode只能传递一个参数");
        }
    }
}
